package com.fjw.car_send;

import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class TouchPadBase extends SurfaceView implements SurfaceHolder.Callback {

    protected Paint mPaint;

    private boolean mRunning;
    private DrawingThread mDrawingThread;

    protected int mFBLevel = 0;
    protected int mLastFBLevel = 1;
    protected int mLRLevel = 0;
    protected int mBeep = 0;
    protected int mLastBeep = 0;
    protected int mLed = 0;
    protected int mLedEventID = -1;
    protected final int MAX_BEEP_INDEX = 4;
    private boolean mTurnRounding = false;

    private Runnable mTurnRound = new Runnable() {
        @Override
        public void run() {
            int state = 0;
            int intervals = 1000;
            while (mTurnRounding) {
                switch (state) {
                    case 0:
                        mFBLevel = 1;
                        mLRLevel = 3;
                        break;
                    case 1:
                    case 3:
                        mFBLevel = 0;
                        mLRLevel = 0;
                        break;
                    case 2:
                        mFBLevel = -1;
                        mLRLevel = -3;
                        break;

                }
                sendData();
                try {
                    Thread.sleep(intervals);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                state++;
                state = state % 4;
            }
        }
    };

    final String LOG_TAG = "car_send_touchpad_base";

    public TouchPadBase(Context context) {
        super(context);
        init();
    }

    public TouchPadBase(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TouchPadBase(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public TouchPadBase(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    public boolean ismRunning() {
        return mRunning;
    }

    public boolean ismTurnRounding() {
        return mTurnRounding;
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.GREEN);
        mPaint.setStyle(Paint.Style.STROKE);
        ;
        mPaint.setStrokeWidth(1);
        mPaint.setAntiAlias(true);

        SurfaceHolder surfaceHolder = getHolder();
        surfaceHolder.addCallback(this);

        setZOrderOnTop(false);
        surfaceHolder.setFormat(PixelFormat.TRANSPARENT);
    }

    protected void startTurnRound() {
        mTurnRounding = true;
        new Thread(mTurnRound).start();
    }

    protected void stopTurnRound() {
        mTurnRounding = false;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mRunning = true;
        mDrawingThread = new DrawingThread(holder);
        mDrawingThread.start();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        mRunning = false;
        while (retry) {
            try {
                mDrawingThread.join();
                retry = false;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private class DrawingThread extends Thread {
        private SurfaceHolder mSurfaceHolder;

        public DrawingThread(SurfaceHolder surfaceHolder) {
            mSurfaceHolder = surfaceHolder;
        }

        @Override
        public void run() {
            Canvas canvas;
            while (mRunning) {
                canvas = null;
                try {
                    canvas = mSurfaceHolder.lockCanvas(null);
                    synchronized (mSurfaceHolder) {
                        doDraw(canvas);
                    }
                } finally {
                    if (canvas != null) {
                        mSurfaceHolder.unlockCanvasAndPost(canvas);
                    }
                }
            }
        }
    }

    protected void doDraw(Canvas canvas) {

    }

    public void sendData() {
        MainActivity.getInstance().sendData(mFBLevel, mLRLevel, mBeep, mLed);
    }
}
